<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This object should represent somehting similar to:
 * <thr:in-reply-to ref="mimime:1281104680" href="http://identi.ca/notice/45262687"></thr:in-reply-to>
 * 
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Thr_InReplyTo implements Atom_ChildElement {
    
    const ELEMENT_IN_REPLY_TO = "in-reply-to";
    
    const ATTRIBUTE_REF = "ref";
    const ATTRIBUTE_HREF = "href";
    
    private $ref = "";
    private $href = "";
    
    public function __construct($ref, $href) {
        $this->setRef($ref);
        $this->setHref($href);
    }
    
    private function setRef($ref) {
        if(empty($ref)) {
            throw new Exception("Ref cannot be set empty!");
        }
        $this->ref = $ref;
    }
    
    public function getRef() {
        return $this->ref;
    }
    
    private function setHref($href) {
        //if(empty($href)) {
        //    throw new Exception("href cannot be set empty!");
        //}
        $this->href = $href;
    }
    
    public function getHref() {
        return $this->href;
    }
    
    /**
     * This method should produce something like this:
     * 
     * <thr:in-reply-to ref="mimime:1281104680" href="http://identi.ca/notice/45262687"/>
     * 
     * @param DOMDocument$dom
     */
    public function asDomElement(DOMDocument $dom) {
        $element = $dom->createElement(Thr::NS_PREFIX . ':' . self::ELEMENT_IN_REPLY_TO);
        $element->setAttribute(self::ATTRIBUTE_REF, $this->ref);
        $element->setAttribute(self::ATTRIBUTE_HREF, $this->href);
        return $element;
    }
    
    public static function buildFromDOMElement(DOMElement $elm) {
        $href = $elm->getAttribute("href");
        $rel = $elm->getAttribute("ref");
        return new Thr_InReplyTo($rel, $href);
    }
}